package com.huike.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.constants.ActivityConstant;
import com.huike.dto.ActivityDto;
import com.huike.entity.Activity;
import com.huike.entity.SysDictData;
import com.huike.exception.DeletionNotAllowedException;
import com.huike.mapper.ActivityMapper;
import com.huike.mapper.SysDictDataMapper;
import com.huike.result.PageResult;
import com.huike.result.Result;
import com.huike.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
@Transactional
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Override
    public PageResult findAll(ActivityDto dto) {

        IPage page = new Page(dto.getPage(), dto.getPageSize());
        LambdaQueryWrapper<Activity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (dto.getNumber() != null) {
            lambdaQueryWrapper.like(Activity::getNumber, dto.getNumber());
        }




        if (dto.getChannel() != null ) {
            lambdaQueryWrapper.eq(Activity::getChannel,dto.getChannel());
        }






        if (dto.getAcbegintime() != null || dto.getAcendtime() != null) {
            lambdaQueryWrapper.ge(Activity::getAcbegintime, dto.getAcbegintime());
            lambdaQueryWrapper.le(Activity::getAcendtime, dto.getAcendtime());
        }
        if (dto.getCreatedbegintime() != null || dto.getCreatedendtime() != null) {
            lambdaQueryWrapper.ge(Activity::getCreatedbegintime, dto.getCreatedbegintime());
            lambdaQueryWrapper.le(Activity::getCreatedendtime, dto.getCreatedendtime());
        }
        if (page.getSize() > 10) {
            page.setSize(10);
        }


        IPage page1 = activityMapper.selectPage(page, lambdaQueryWrapper);
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());

        return pageResult;
    }

    public static final Integer INTRO_WORD_NUMBER = 10;

    @Override
    public void add(ActivityDto dto) {
        notnoll(dto);
        if (dto.getIntro().length() > INTRO_WORD_NUMBER) {
            throw new DeletionNotAllowedException("简介字数10个字以内");
        }
        Activity activity = new Activity();
        BeanUtils.copyProperties(dto, activity);
        String activityClues = "activityClues";
        if (dto.getDetail() == "0") {
            Result channel = channel(activityClues);
        }
        if (dto.getDetail() == "1") {
            Result channel = channel(activityClues);
        }
        activity.setCreatedbegintime(new Date());
        activity.setCreatedendtime(new Date());
        activityMapper.insert(activity);

    }


    public static final String ACTIVITY_DELETE = "活动正在进行中,不能删除,请先结束活动";

    @Override
    public void delete(Integer ids) {
        /*for (Integer id : ids) {
            Activity activity = activityMapper.selectById(id);
            if (activity.getStatus() == 0) {
                throw new DeletionNotAllowedException(ACTIVITY_DELETE);
            }*/
       /* Activity activity = activityMapper.selectById(ids);
        if (activity.getStatus()==0){
            throw new DeletionNotAllowedException(ACTIVITY_DELETE);
        }*/
        Activity activity = activityMapper.selectById(ids);
        if (activity.getStatus() == 0) {
            throw new DeletionNotAllowedException(ACTIVITY_DELETE);
        }
        activityMapper.deleteById(ids);

    }

    @Override
    public void updateactivity(ActivityDto dto) {

        Activity activity = new Activity();
        BeanUtils.copyProperties(dto, activity);
        String activityClues = "activityClues";
        if (dto.getDetail() == "0") {
            Result channel = channel(activityClues);
        }
        if (dto.getDetail() == "1") {
            Result channel = channel(activityClues);
        }
        activityMapper.updateById(activity);
    }

    @Override
    public Result clusm(String activityClues) {

        LambdaQueryWrapper<SysDictData> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDictData::getDictType, activityClues);
        List<SysDictData> sysDictData = sysDictDataMapper.selectList(lambdaQueryWrapper);
        return Result.success(sysDictData);
    }

    @Override
    public Result channel(String activitychannel) {
        LambdaQueryWrapper<SysDictData> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDictData::getDictType, activitychannel);
        List<SysDictData> sysDictData = sysDictDataMapper.selectList(lambdaQueryWrapper);
        return Result.success(sysDictData);
    }


    @Override
    public Result findById(Integer id) {

        Activity activity = activityMapper.selectById(id);
        String activityClues = "activityClues";
        if (activity.getDetail() == "0") {
            Result channel = channel(activityClues);
            activity.setDetail(JSON.toJSONString(channel));
        }
        if (activity.getChannel() == "1") {
            Result channel = channel(activityClues);
            activity.setDetail(JSON.toJSONString(channel));
        }
        return Result.success(activity);
    }


    private void notnoll(ActivityDto dto) {
        if (dto.getChannel() == null) {
            throw new ArithmeticException(ActivityConstant.ACTIVITY_SAVE_CHANNEL);
        }
        if (dto.getIntro() == null) {
            throw new ArithmeticException(ActivityConstant.ACTIVITY_SAVE_NUMBER);
        }
        if (dto.getDetail() == null) {
            throw new ArithmeticException(ActivityConstant.ACTIVITY_SAVE_MOLD);
        }
        if (dto.getDiscount() == null) {
            throw new ArithmeticException(ActivityConstant.ACTIVITY_SAVE_DISCOUNT);
        }
        if (dto.getTicket() == null) {
            throw new ArithmeticException(ActivityConstant.ACTIVITY_SAVE_TICKET);
        }
        if (dto.getAcbegintime() == null) {
            throw new ArithmeticException(ActivityConstant.ACTIVITY_SAVE_ACBEGINTIME);
        }
        if (dto.getAcendtime() == null) {
            throw new ArithmeticException(ActivityConstant.ACTIVITY_SAVE_ACENDTIME);
        }
    }
}
